package com.wowudi.controller;

import com.wowudi.entity.User;
import com.wowudi.dto.Result;
import com.wowudi.service.FileService;
import com.wowudi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@RestController
@Api(tags = "用户管理")
@SessionAttributes("user")
@RequestMapping("api/system/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;


    @ApiOperation(value = "用户登录")
    @PostMapping("login/{name}/{pwd}")
    public Result login(@PathVariable("name") String name, @PathVariable("pwd") String pwd){
        Result result = userService.login(name, pwd);
        return result;
    }


    @ApiOperation(value = "修改用户信息")
    @PostMapping("updateUser")
    public Result updateUser(@RequestBody User user){
        Result result = userService.updateUser(user);
        return result;
    }

    @ApiOperation(value = "查询所有用户信息")
    @GetMapping("findPageUser")
    public Result findPageUser(String userName,Integer page,Integer limit){
        Result result = userService.findPageUser(userName,page,limit);
        return result;
    }


    @ApiOperation(value = "新增用户信息")
    @PostMapping("addUser")
    public Result addUser(@RequestBody User user){
        Result result = userService.addUser(user);
        return result;
    }


    @ApiOperation(value = "删除用户信息")
    @PostMapping("deleteUser")
    public Result deleteUser(Integer userId){
        Result result = userService.deleteUser(userId);
        return result;
    }


    //上传文件到阿里云oss
    @ApiOperation(value = "上传头像")
    @PostMapping("fileUpload")
    public Result fileUpload(MultipartFile file,long userId){
        // 获取上传的文件
        String url = fileService.fileUpload(file);
        userService.updateAvatar(userId,url);
        Result result = new Result();
        result.setMessage("成功");
        return result;
    }


}
